package com.cx.service.lq.Impl;

import com.cx.mapper.FriendMapper;
import com.cx.mapper.GameMapper;
import com.cx.mapper.GamelibraryMapper;
import com.cx.pojo.*;
import com.cx.service.lq.FriendService;
import com.cx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    FriendMapper friendMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GameMapper gameMapper;
    @Autowired
    GamelibraryMapper gamelibraryMapper;

    @Override
    public List<FriendInfo> selectAllFriend(int userId) {
        List<FriendInfo> friendInfos = friendMapper.selectAll(userId);
        return friendInfos;
    }

    @Override
    public int addfriendByphone(int userId,String userPhone) {
        User user = userMapper.selectByPhone(userPhone);
        System.out.println(user);
        Friend friend  = new Friend();
        friend.setFriendUserId(user.getUserId());
        friend.setUserId(userId);
        Friend friend1  = new Friend();
        friend1.setFriendUserId(userId);
        friend1.setUserId(user.getUserId());
        int affect = friendMapper.insert(friend);
        int affect1 = friendMapper.insert(friend1);
        return affect+affect1;
    }

    @Override
    public int deleteByfriend(int friendUserId) {
        int  affect = friendMapper.deleteByfriend(friendUserId);
        return affect;
    }

    @Override
    public int givegame(int friendUserId,int gameId) {
        Date date = new Date();
        Game game= gameMapper.selectByPrimaryKey(gameId);
        Gamelibrary gamelibrary = new Gamelibrary();
        gamelibrary.setGameId(gameId);
        gamelibrary.setGlPrice(game.getGamePrice()*game.getGameDiscount());
        gamelibrary.setUserId(friendUserId);
        gamelibrary.setGlTime(date);
        int affect = gamelibraryMapper.insert(gamelibrary);
        return affect;
    }


}
